package model;

import java.awt.image.BufferedImage;
import java.awt.image.WritableRaster;

import javax.swing.ImageIcon;

public class CollisionUtils{

	public static boolean intersects(Collisionable s1,Collisionable s2){
		int[] interReg = getInterRegion(s1,s2);
		//System.out.println(interReg);
		if(interReg==null)return false;
		else{
			
			CollisionMap extract1=s1.getCollisionMap().getExtract(interReg[0]-s1.getPosition()[0], interReg[1]-s1.getPosition()[1], interReg[2]-s1.getPosition()[0], interReg[3]-s1.getPosition()[1]);
			CollisionMap extract2=s2.getCollisionMap().getExtract(interReg[0]-s2.getPosition()[0], interReg[1]-s2.getPosition()[1], interReg[2]-s2.getPosition()[0], interReg[3]-s2.getPosition()[1]);
			//System.out.println(s2.getCollisionMap().getWidth()+","+s2.getCollisionMap().getHeight());
			//System.out.println("toExtract:("+(interReg[0]-s2.getPosition()[0])+","+(interReg[1]-s2.getPosition()[1])+")("+(interReg[2]-s2.getPosition()[0])+","+(interReg[3]-s2.getPosition()[1])+")");

			//System.out.print("collMap1:\n"+s1.getCollisionMap());
			//System.out.print("collMap2:\n"+s2.getCollisionMap());
			//System.out.print("extract1:\n"+extract1);
			//System.out.print("extract2:\n"+extract2);
			//return false;
			
			return intersects(extract1,extract2);
		}
		
	}
	
	public static boolean intersects(CollisionMap cm1,CollisionMap cm2){
		for(int x=0;x<cm1.getWidth();x++){
			for(int y=0;y<cm1.getHeight();y++){
				if(cm1.get(x, y)>0 && cm2.get(x, y)>0)return true;
			}
		}
		return false;
	}
	private static int[] getInterRegion(Collisionable s1,Collisionable s2){
		if(s1!=null && s2!=null ){
			// dim[origx,origy,tox,toy]
			int[] dim1 = {s1.getPosition()[0],s1.getPosition()[1],s1.getPosition()[0]+s1.getCollisionMap().getWidth(),s1.getPosition()[1]+s1.getCollisionMap().getHeight()};
			int[] dim2 = {s2.getPosition()[0],s2.getPosition()[1],s2.getPosition()[0]+s2.getCollisionMap().getWidth(),s2.getPosition()[1]+s2.getCollisionMap().getHeight()};
		
			int[] retDim = new int[4];
			
			//place l'origx
			if(dim1[0]>=dim2[0]&&dim1[0]<=dim2[2]){
				retDim[0]= dim1[0];
			}else if(dim2[0]>=dim1[0]&&dim2[0]<=dim1[2]){
				retDim[0]= dim2[0];
			}else return null;
			

			//place l'origy
			if(dim1[1]>=dim2[1]&&dim1[1]<=dim2[3]){
				retDim[1]= dim1[1];
			}else if(dim2[1]>=dim1[1]&&dim2[1]<=dim1[3]){
				retDim[1]= dim2[1];
			}else return null;
			

			//place le tox
			if(dim1[2]>=dim2[0]&&dim1[2]<=dim2[2]){
				retDim[2]= dim1[2];
			}else if(dim2[2]>=dim1[0]&&dim2[2]<=dim1[2]){
				retDim[2]= dim2[2];
			}else return null;
			

			//place le toy
			if(dim1[3]>=dim2[1]&&dim1[3]<=dim2[3]){
				retDim[3]= dim1[3];
			}else if(dim2[3]>=dim1[1]&&dim2[3]<=dim1[3]){
				retDim[3]= dim2[3];
			}else return null;
			
			//System.out.println("("+retDim[0]+","+retDim[1]+"),("+retDim[2]+","+retDim[3]);
			return retDim;
		}else{
			return null;
		}
		
	}
	public static ImageIcon getCollisionMapImage(CollisionMap collMap,int[] color){
		BufferedImage bimage = new BufferedImage(collMap.getWidth(), collMap.getHeight(), BufferedImage.TYPE_INT_ARGB);
		WritableRaster r = bimage.getRaster();
		
		int[] transp = {0,0,250,75};
		int[] opaque = {0,250,0,50};
		if(color!=null) opaque = color;
		
		for (int x = 0; x < collMap.getWidth(); x++) {
			for (int y = 0; y < collMap.getHeight(); y++) {
				if(collMap.get(x, y)==0)
					r.setPixel(x, y, transp);
				else
					r.setPixel(x, y, opaque);
			}
		}
		return new ImageIcon(bimage);
	}
}
